package com.learn.seven;

import org.apache.log4j.Logger;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/15
 */
public class TestSequence2 {

    static final Logger log=Logger.getLogger(TestSequence2.class);

    static ReentrantLock reentrantLock=new ReentrantLock();
    static Condition t1condition=reentrantLock.newCondition();
    static Condition t2condition=reentrantLock.newCondition();

    static boolean t2runned = false;

    public static void main(String[] args) {

        new Thread(()->{
            reentrantLock.lock();
            try {
                while (!t2runned){
                    try {
                        t1condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("打印1");
            }finally {
                reentrantLock.unlock();
            }

        },"t1").start();


        new Thread(()->{
            reentrantLock.lock();
            try {
                log.debug("打印2");
                t2runned=true;
                t1condition.signal();
            }finally {
                reentrantLock.unlock();
            }
        },"t2").start();
    }

}
